package com.bl.order.controller;

import com.bl.basic.util.LoginContext;
import com.bl.order.dto.OrderDto;
import com.bl.order.service.IOrderPetAcquisitionService;
import com.bl.order.domain.OrderPetAcquisition;
import com.bl.order.query.OrderPetAcquisitionQuery;
import com.bl.basic.util.JsonResult;
import com.bl.basic.util.PageList;
import com.bl.user.domain.Logininfo;
import net.sf.jsqlparser.statement.select.KSQLJoinWindow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/orderPetAcquisition")
public class OrderPetAcquisitionController {

    @Autowired
    public IOrderPetAcquisitionService orderPetAcquisitionService;

    /**
     * 接口：添加或修改
     * @param orderPetAcquisition  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public JsonResult addOrUpdate(@RequestBody OrderPetAcquisition orderPetAcquisition){
        try {
            if( orderPetAcquisition.getId()!=null)
                orderPetAcquisitionService.update(orderPetAcquisition);
            else
                orderPetAcquisitionService.save(orderPetAcquisition);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"操作失败");
        }
    }


    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public JsonResult patchDelete(@RequestBody Long[] ids){
        try {
                orderPetAcquisitionService.patchDelete(ids);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public OrderPetAcquisition findOne(@PathVariable("id")Long id) {
        return orderPetAcquisitionService.loadById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<OrderPetAcquisition> loadAll(){
        return orderPetAcquisitionService.loadAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<OrderPetAcquisition> queryPage(@RequestBody OrderPetAcquisitionQuery query, HttpServletRequest request ) {

        Logininfo loginUser = LoginContext.getLoginUser(request);


        return orderPetAcquisitionService.queryPage(query,loginUser.getId());
    }

    /**
     *  接口：订单确认
     * @param dto
     * @return
     */
    @PostMapping("/confirm")
    public JsonResult confirm(@RequestBody OrderDto dto) {
        try {
            orderPetAcquisitionService.confirm(dto);
            return   JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMessage("订单异常");
        }
    }
    /**
     * 接口：取消订单
     * @param id
     * @return AjaxResult 响应给前端
     */
    @DeleteMapping(value="/{id}")
    public JsonResult cancel(@PathVariable("id") Long id){
        try {
            orderPetAcquisitionService.cancel(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMessage("订单异常，取消失败");
        }
    }

}
